<template>
  <div class="access-log-container">
    <el-card>
      <!-- 搜索栏 -->
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="IP地址">
          <el-input v-model="queryParams.ip" placeholder="请输入IP地址" clearable />
        </el-form-item>
        <el-form-item label="访问时间">
          <el-date-picker
            v-model="queryParams.timeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="logList" v-loading="loading" style="width: 100%">
        <el-table-column prop="ip" label="IP地址" />
        <el-table-column prop="location" label="地理位置" />
        <el-table-column prop="userAgent" label="浏览器信息" />
        <el-table-column prop="url" label="访问路径" />
        <el-table-column prop="visitCount" label="访问次数" width="100" align="center"/>
        <el-table-column prop="createdAt" label="最后访问时间" width="180" align="center">
          <template #default="{ row }">
            {{ formatDateTime(row.createdAt) }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        class="pagination"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        layout="total, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { getAccessLogs } from '@/http/apis/access-log'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

interface QueryParams {
  ip: string
  timeRange: string[]
  pageNum: number
  pageSize: number
}

const loading = ref(false)
const logList = ref([])
const total = ref(0)
const queryParams = reactive<QueryParams>({
  ip: '',
  timeRange: [],
  pageNum: 1,
  pageSize: 10
})

const router = useRouter()

// 获取访问日志列表
const getList = async () => {
  try {
    loading.value = true
    const { data } = await getAccessLogs(queryParams)
    logList.value = data.list
    total.value = data.total
  } catch (error: any) {
    if (error.response?.status === 401) {
      ElMessage.error('请先登录')
      router.push('/login')
    } else {
      ElMessage.error(error.message || '获取访问日志失败')
    }
    logList.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 查询按钮点击
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置查询
const resetQuery = () => {
  queryParams.ip = ''
  queryParams.timeRange = []
  queryParams.pageNum = 1
  getList()
}

// 页码改变
const handleCurrentChange = (page: number) => {
  queryParams.pageNum = page
  getList()
}

// 添加时间格式化函数
const formatDateTime = (timestamp: string) => {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.access-log-container {
  padding: 20px;
  width: 100%;
  height: 100%;
}
.search-form {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 